package com.ymatou.iapi.productmanager.testcase;

import com.ymatou.iapi.productmanager.parameter.BatchDeleteProductsBean;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productmanager.service.AttachProductToLivesCall;
import com.ymatou.iapi.productmanager.service.BatchDeleteProductsCall;
import com.ymatou.mq.admin.parameter.MessageListBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.*;
import com.ymttest.database.mongo.jproduct.ActivityProductsWapper;
import com.ymttest.database.sqlwapper.YmtLiveWapper;
import com.ymttest.database.sqlwapper.YmtProductActivityStockWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.database.sqlwapper.YmtProductsInLiveWapper;
import com.ymttest.utils.EnvSetup;
import org.json.JSONObject;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Ts_BatchDeleteProducts {
	private static BatchDeleteProductsCall batchDeleteProductsCall = new BatchDeleteProductsCall();
	private static BatchDeleteProductsBean batchDeleteProductsBean = new BatchDeleteProductsBean();
	private static ActivityProductsWapper activityProductsWapper = new ActivityProductsWapper();
	private static YmtProductsInLiveWapper ymtProductsInLiveWapper = new YmtProductsInLiveWapper();
	private static YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();
	private static YmtLiveWapper ymtLiveWapper = new YmtLiveWapper();
	private static YmtProductActivityStockWapper ymtProductActivityStockWapper = new YmtProductActivityStockWapper();
	private static Integer sellerId1 = Integer.valueOf(EnvSetup.getData("productsellerid"));
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	private static String sellerName = EnvSetup.getData("productsellername");	

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("批量删除商品");
//		//SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(sellerId);
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(batchDeleteProductsCall.getOpurl());
	}
	@After
	public void caseDown() {
		Logger.end();
	}

	public void verifyMq(List<String> productIds) throws IOException {
		productIds.forEach(productId->{
			MessageListBean messageListBean = MqQueryCallService.initGroupon(productId, "product_delete");
			List<Map> joa2 = null;
			try {
				joa2 = MqQueryCallService.GetMqMessage(messageListBean);
			} catch (IOException e) {
				e.printStackTrace();
			}
			Logger.verifyEquals(productId,joa2.get(0).get("productId").toString(),"验证ProductId");
			Logger.verifyEquals(sellerId,Integer.parseInt(joa2.get(0).get("sellerId").toString()),"验证ProductId");
		});
	}

	/**
	 * 商品删除action=-2  同步服务改
	 * 活动商品不能删除
	 * 直播商品删除status=-2
	 * @2018/09/04
	 */
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchDeleteProducts_001() {
		Logger.start(true,"删除多个商品");
		try {
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV3.AddSellerProduct(addProdReq);
			String productId1 = ProductManagerCallServiceV3.AddSellerProduct(addProdReq);
			List<String> list = new ArrayList<String>();
			list.add(productId);
			list.add(productId1);
			batchDeleteProductsBean.setProductIds(list);
			batchDeleteProductsBean.setUserId(sellerId);  
			batchDeleteProductsCall.setData(batchDeleteProductsBean);
			JSONObject ret = batchDeleteProductsCall.callAndGetReturnData(batchDeleteProductsBean);
			checkDeleteProduct(ret,list);
			verifyMq(list);
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_BatchDeleteProducts_002() {
		Logger.start(true,"删除多个商品,包含正在进行直播商品");
		try {
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProductOnSale(addProdReq).get("Data").getAsJsonObject().get("ProductId").getAsString();
			String productId1 = ProductManagerCallServiceV2.addNewProductOnSale(addProdReq).get("Data").getAsJsonObject().get("ProductId").getAsString();
			List<Integer> liveIdList = LiveCallService.createLivesInProcessAndInFuture(sellerId1, sellerName, 2, 3);
			new AttachProductToLivesCall().call(sellerId, productId,liveIdList);
			new AttachProductToLivesCall().call(sellerId, productId1,liveIdList);
			ArrayList<String> list = new ArrayList<String>();
			list.add(productId);
			list.add(productId1);
			batchDeleteProductsBean.setProductIds(list);
			batchDeleteProductsBean.setUserId(sellerId);
			batchDeleteProductsCall.setData(batchDeleteProductsBean);
			JSONObject ret = batchDeleteProductsCall.callAndGetReturnData(batchDeleteProductsBean);
			checkDeleteProductInLive(ret,list);
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_BatchDeleteProducts_003() {
		Logger.start(true,"删除多个商品,包含将来直播商品");
		try {
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProductOnSale(addProdReq).get("Data").getAsJsonObject().get("ProductId").getAsString();
			String productId1 = ProductManagerCallServiceV2.addNewProductOnSale(addProdReq).get("Data").getAsJsonObject().get("ProductId").getAsString();
			List<Integer> liveIdList = LiveCallService.createLivesInFuture(sellerId1, sellerName, 2, 3);
			new AttachProductToLivesCall().call(sellerId, productId,liveIdList);
			new AttachProductToLivesCall().call(sellerId, productId1,liveIdList);
			ArrayList<String> list = new ArrayList<String>();
			list.add(productId);
			list.add(productId1);
			batchDeleteProductsBean.setProductIds(list);
			batchDeleteProductsBean.setUserId(sellerId);
			batchDeleteProductsCall.setData(batchDeleteProductsBean);
			JSONObject ret = batchDeleteProductsCall.callAndGetReturnData(batchDeleteProductsBean);
			checkDeleteProductInLive(ret,list);
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	

	public static void checkDeleteProduct(JSONObject ret,List<String> productIdList) throws Exception{
		Logger.verifyEquals(200, ret.getInt("Code"), "返回code应为200");
		for (String productId : productIdList) {
			List<Map> productIdsqllist = new YmtProductsIWapper().selectProductByProductIdforMap(productId);
			Logger.verifyEquals(-2, Integer.parseInt(productIdsqllist.get(0).get("iAction").toString()), "删除商品 iaction为-2");
		}
	}
	
	public static void checkDeleteProductInLive(JSONObject ret,List<String> productIdList) throws Exception{
		Logger.verifyEquals(200, ret.getInt("Code"), "返回code应为200");
		for (String productId : productIdList) {
			List<Map> liveProductMapList = ymtProductsInLiveWapper.selectInLiveByProductId(productId);
			for(int j=0;j<liveProductMapList.size();j++){
				Map liveProductMap = liveProductMapList.get(j);
				int liveId = Integer.parseInt(liveProductMap.get("iActivityId").toString());
				 
				Logger.verifyEquals(-1, Integer.parseInt(liveProductMap.get("iAction").toString()), "删除商品，iaction应为-1 liveId:"+ liveId
						+", productId:" + productId);
				//默认删除后，直播中没有有效商品
				Map liveMap = ymtLiveWapper.selectforMapByLiveId(liveId).get(0);
				Logger.verifyEquals(0, Integer.parseInt(liveMap.get("ProductCount").toString()), "删除后, productcount应为0,liveId:"+ liveId);
				Logger.verifyEquals(0, Integer.parseInt(liveMap.get("PutawayCount").toString()), "删除后, PutawayCount应为0,liveId:"+ liveId);
				
				NewJavaMongoCheckService.checkLiveMgoSyncBySQL_NewJavaMgo(liveId,false);
			}
		}
	}
}
